package com.springboot.astra.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.astra.bean.Goods;
import com.springboot.astra.bean.Order;
import com.springboot.astra.bean.Orderitem;
import com.springboot.astra.bean.Shopcart;
import com.springboot.astra.result.DataResult;
import com.springboot.astra.service.GoodsService;
import com.springboot.astra.service.OrderService;
import com.springboot.astra.service.OrderitemService;
import com.springboot.astra.service.ShopcartService;
import com.springboot.astra.util.DateUtils;
import com.springboot.astra.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    OrderService orderService;
    @Autowired
    ShopcartService shopcartService;
    @Autowired
    OrderitemService orderitemService;
    @Autowired
    GoodsService goodsService;

    @PostMapping("/getMyOrder")
    public DataResult getMyOrder(@RequestParam Map<String,String> map,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Claims claims = jwtUtils.parseJwt(token);
        Integer userId = Integer.valueOf(claims.getId());
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Order> page = new Page<Order>(pagenum,pagesize);
        IPage<Order> orderPage = orderService.getMyOrder(page,userId);
        return DataResult.success(orderPage);
    }

    @PostMapping("/getAllOrder")
    public DataResult getAllOrder(@RequestParam Map<String,String> map) {
        String query = map.get("query");
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Order> page = new Page<Order>(pagenum,pagesize);
        IPage<Order> orderPage = orderService.getOrderPageVo(page,query);
        return DataResult.success(orderPage);
    }
    @PostMapping("/getOneOrder")
    public DataResult getOneOrder(@RequestParam Map<String,String> map){
        Integer orderId = Integer.valueOf(map.get("orderId"));
        Order order = orderService.getOneByOrderId(orderId);
        return DataResult.success(order);
    }
    @PutMapping("/updateOrder")
    public DataResult updateOrder(@RequestBody Order order) {
        order.setUpdateTime(DateUtils.getTime());
        orderService.updateOnrByOrderId(order);
        return DataResult.success();
    }
    @PostMapping("/addOrderForOneGoods")
    @Transactional
    public DataResult addOrderForOneGoods(@RequestParam Map<String,String> map,HttpServletRequest request){
        Integer goodsId = Integer.valueOf(map.get("goodsId"));
        Goods goods = goodsService.getOneById(goodsId);
        //判断商品是否下架
        if (!goods.isDelete()){
            return DataResult.failWithMsg("该商品已被下架："+goods.getGoodsName());
        }
        String token = request.getHeader("Authorization");
        Claims claims = jwtUtils.parseJwt(token);
        Integer userId = Integer.valueOf(claims.getId());
        //生成新的订单
        Order order = new Order();
        order.setOrderNo(RandomUtil.randomString(11));
        order.setUserId(userId);
        order.setTotal(goods.getGoodsPrice());
        order.setOrderStatus(0);
        order.setPayType(1);
        order.setDelete(true);
        order.setCreateTime(DateUtils.getTime());
        order.setUpdateTime(DateUtils.getTime());
        order.setPayTime(DateUtils.getTime());
        orderService.insertOrder(order);
        //生成订单项
        Orderitem orderItem = new Orderitem();
        orderItem.setOrderId(order.getOrderId());
        orderItem.setGoodsId(goods.getGoodsId());
        orderItem.setGoodsCount(1);
        orderItem.setCreateTime(DateUtils.getTime());
        orderitemService.insertOrderitem(orderItem);
        Order newOrder = orderService.getOneByOrderId(order.getOrderId());
        return DataResult.success(newOrder.getOrderId());
    }
    @GetMapping("/addOrder")
    @Transactional
    public DataResult addOrder(HttpServletRequest request) {
        Double total = 0.0;
        String token = request.getHeader("Authorization");
        Claims claims = jwtUtils.parseJwt(token);
        Integer userId = Integer.valueOf(claims.getId());
        List<Shopcart> shopcartList = shopcartService.getAllByUserId(userId);
        for (Shopcart item: shopcartList) {
            Goods goods = item.getGoods();
            //判断商品是否下架
            if (!goods.isDelete()){
                return DataResult.failWithMsg("购物车内含被下架商品："+goods.getGoodsName());
            }
            total = total + goods.getGoodsPrice();
        }
        Order order = new Order();
        order.setOrderNo(RandomUtil.randomString(11));
        order.setUserId(userId);
        order.setOrderStatus(0);
        order.setTotal(total);
        order.setPayType(1);
        order.setDelete(true);
        order.setCreateTime(DateUtils.getTime());
        order.setUpdateTime(DateUtils.getTime());
        order.setPayTime(DateUtils.getTime());
        orderService.insertOrder(order);
        for (Shopcart item: shopcartList) {
            Orderitem orderItem = new Orderitem();
            Goods goods = item.getGoods();
            orderItem.setOrderId(order.getOrderId());
            orderItem.setGoodsId(goods.getGoodsId());
            orderItem.setGoodsCount(1);
            orderItem.setCreateTime(DateUtils.getTime());
            orderitemService.insertOrderitem(orderItem);
        }
        //清空购物车
        for (Shopcart item: shopcartList) {
            shopcartService.deleteOneById(item.getShopCartId());
        }
        return DataResult.success(order.getOrderId());
    }
    @PostMapping("updateOrderTrue")
    public DataResult updateOrderTrue(@RequestParam Map<String,String> map) {
        Integer orderId = Integer.valueOf(map.get("orderId"));
        Order order = orderService.getOneByOrderId(orderId);
        order.setOrderStatus(1);
        orderService.updateOnrByOrderId(order);
        //获取所有orderItem添加cdk
        List<Orderitem> orderitemList = order.getOrderitems();
        //模拟发货
        for (Orderitem ot: orderitemList) {
            ot.setCdk(RandomUtil.randomString(16));
            orderitemService.updateCdk(ot);
        }
        return DataResult.success();
    }
    @PostMapping("/getOrderItem")
    public DataResult getOrderItem(@RequestParam Map<String,String> map) {
        Integer orderId = Integer.valueOf(map.get("orderId"));
        List<Orderitem> OrderitemList = orderitemService.getAllByOrderId(orderId);
        return DataResult.success(OrderitemList);
    }
}
